<?php
class Model extends Db {

    // 读取指定文章附件
    public function getAttachment( $id ) {
        $this->table = 'attachment';
        return $this->findAll( array( 'aid' => $id ) );
    }

    /*
     * 读取$tag 所关联的文章
     *
     * @param $tag      关联的标签
     * @param $id       排除ID以外的文章
     * @param $limit    返回数量
     */
    public function article_related( $tag, $id, $limit ) {
        $tag = "'" . implode( "','", $tag ) . "'";
        $sql = "SELECT a.* FROM {$this->t('tag')} AS t
                LEFT JOIN {$this->t('mark')} AS m ON ( t.id = m.tid )
                LEFT JOIN {$this->t('article')} AS a ON ( a.id = m.aid )
                WHERE t.tag IN ( $tag )";
        $sql = $this->_db->setlimit( $sql, $limit );

        if ( $result = $this->findSql( $sql ) ) {
            foreach ( $result as $v ) {
                $tmp[ $v[ 'id' ] ] = $v;
            }
            unset( $result );
        }

        if ( isset( $tmp[ $id ] ) ) {
            unset( $tmp[ $id ] );
        }
        return $tmp;
    }

    /*
     * 返回评论与文章
     * @param $limit    返回数量
     */
    public function article_comment( $limit = null ) {
        $sql = "SELECT a.id as cid, a.aid as id, a.content, a.dateline, a.author, a.email, b.model, b.alias
                FROM  `{$this->t('comment')}` AS a
                LEFT JOIN `{$this->t('article')}` AS b ON ( a.aid = b.id )
                ORDER BY a.id DESC";
        if ( $limit != null ) $sql = $this->_db->setlimit( $sql, $limit );
        return $this->findSql( $sql );
    }

    /*
     * 返回指定文章ID的标签
     */
    public function tag_id( $id ) {
        $sql = "SELECT GROUP_CONCAT( t.tag ) as tag
                FROM {$this->t('mark')} AS m
                LEFT JOIN {$this->t('tag')} AS t ON ( m.tid = t.id )
                WHERE m.aid = {$id}";
        if ( $tag = $this->findSql( $sql ) ) {
            return $tag[ 0 ][ 'tag' ];
        }
        return '';
    }

    public function tag_limit( $limit = null ) {
        $tags = $this->Cache()->get( 'tag' );
        if ( $limit == null ) return $tags;
        return array_slice( $tags, 0, $limit );
    }

    // 修改标签
    public function editTag( $id ) {
        if ( !$tag = R( 'tag' ) ) {
            return;
        }

        // 原文章没有标签 直接添加标签
        if ( !$oldTag = R( 'oldtag' ) ) {
            $this->addTag( $id );
            return;
        }

        // 原文章标签
        $tags = explode( ",", rtrim( $tag, "," ) );

        // 文章修改后标签
        $oldTags = explode( ",", rtrim( $oldTag, "," ) );

        // 计算原文章与文章修改后标签差异
        $addTag = array_diff( $tags, $oldTags );
        $delTag = array_diff( $oldTags, $tags );

        // 删除标签
        foreach ( $delTag as $t ) {
            $this->table = 'tag';
            $where = array( 'tag' => $t );

            $tmp = $this->find( $where );
            if ( $tmp[ 'total' ] > 1 ) {
                // 标签计数>1时 对计数-1
                $this->decrField( $where, 'total' );
            } else {
                // 标签计数<=1时 删除该标签
                $this->delete( $where );
            }

            //删除文章关联记录
            $this->table = 'mark';
            $this->delete( array( 'aid' => $id, 'tid' => $tmp[ 'id' ] ) );
        }


        if ( $addTag ) {
            $_REQUEST[ 'tag' ] = implode( ",", $addTag );
            // 添加标签
            $this->addTag( $id );
        }
    }

    /*
     * 添加标签
     */
    public function addTag( $id ) {
        if ( !$tag = R( 'tag' ) ) {
            return;
        }

        $tags = explode( ",", rtrim( $tag, ',' ) );
        foreach ( $tags as $v ) {
            $this->table = 'tag';
            $where = array( 'tag' => strtolower( $v ) );

            // 查找标签, 如果存在获取标签ID；不存在则创建标签后获取标签ID
            if ( $tmp = $this->find( $where ) ) {
                $tid = $tmp[ 'id' ];
            } else {
                $tid = $this->create( $where );
            }

            // 更新标签计数
            $this->incrField( $where, 'total' );

            // 创建文章与标签关联记录
            $this->table = 'mark';
            $this->create( array( 'aid' => $id, 'tid' => $tid ) );
        }

        // 更新标签缓存
        $this->Cache()->update( 'tag' );
    }
}